package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dongdong.mapper.TbBrandMapper;
import com.dongdong.model.TbBrand;
import com.dongdong.model.TbBrandExample;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.service.BrandService;
import com.dongdong.vo.BrandVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<TbBrandMapper, TbBrand> implements BrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;


    @Override
    public List<BrandVO> getBrandAll() {
//        List<TbBrand> tbBrandList = tbBrandMapper.selectByExample(null);
        List<TbBrand> tbBrandList = list();
        // 把brandList 中的泛型TbBrand转换为BrandVO
/*        ArrayList<BrandVO> vos = new ArrayList<>();
        for (TbBrand tbBrand : tbBrandList){
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(tbBrand, brandVO);
            vos.add (brandVO);
        }*/
        //使用jdk8新特性[1,2,3,4,5,6]
        List<BrandVO> vos = tbBrandList.stream().map((x) -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());//将流转换成list集合
        return vos;
    }

    @Override
    public PageResult<BrandVO> findBrandPage(PageBean<BrandVO> pageBean) {


        TbBrandExample example = new TbBrandExample();
        TbBrandExample. Criteria criteria = example.createCriteria();
        if(!StrUtil.isEmpty(pageBean.getBean().getFirstChar())){
            criteria. andFirstCharLike(  "%"+pageBean.getBean().getFirstChar()+"%");
        }
        if(!StrUtil.isEmpty(pageBean.getBean().getName())){
            criteria. andNameLike("%"+pageBean.getBean().getName()+"%");
        }
        //分页部分
        PageHelper.startPage(pageBean.getPageNumber(),pageBean.getPageSize());
        List<TbBrand> tbBrandList = tbBrandMapper.selectByExample(example);
        PageInfo<TbBrand> info = new PageInfo<>(tbBrandList);

        //转换
         List<BrandVO>  vos = info.getList().stream().map((x) ->  {
         BrandVO vo = new BrandVO();
         BeanUtils. copyProperties(x, vo);
         return vo;
        }). collect (Collectors.toList());
         return new PageResult<>(info.getPageNum(), info.getPageSize(), info.getTotal(), vos);
    }

    @Override
    public void insert(TbBrand tbBrand) {
//        tbBrandMapper.insertSelective(tbBrand);
        save(tbBrand);

    }

    @Override
    public BrandVO selectbyid(Long id) {

//        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        TbBrand tbBrand = getById(id);     //改为mybatisplus

        BrandVO brandVO = new BrandVO();
        BeanUtils.copyProperties(tbBrand,brandVO);
        return brandVO;
    }

    @Override
    public void update(TbBrand tbBrand) {
        //         tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
        updateById(tbBrand);
    }

    @Override
    public void deletebyid(Long id) {
//        tbBrandMapper.deleteByPrimaryKey(id);
        removeById(id);
    }


}
